package one.stand.controller;

import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.CompanyDissolutionRequest;
import com.jmp.base.dto.CompanyQuitRequest;
import com.jmp.feign.CompanyFeign;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import one.stand.model.BusinessCard;
import one.stand.model.ResultModel;
import one.stand.request.BusinessCardRequest;
import one.stand.request.CustomerRequest;
import one.stand.request.UserTaskRequest;
import one.stand.service.BusinessCardBaseService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 公司财务/销售
 *
 * @author Administrator
 */
@RequestMapping("crmApi/userTask")
@RestController
public class UserTaskController extends BaseController {
    @Autowired
    private CompanyFeign companyFeign;
    @Autowired
    private BusinessCardBaseService businessCardBaseService;

    @ApiOperation(value = "录入企业员工本月目标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardId", value = "员工名片id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "targetAmount", value = "目标金额", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "customerNum", value = "独占客户数量", required = true, paramType = "Integer"),
    })
    @PostMapping("addMoneyNum")
    @SecurityAuthCheck(company = true, card = true, isBoss = true)
    public ResultModel addMoneyNum(@RequestAttribute UserTaskRequest request) {
        return crmFegin.addMoneyNum(request);
    }

    @PostMapping("moveCustomer")
    @ApiOperation(value = "企业员工移交客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerIds", value = "客户Id,逗号隔开", required = true, paramType = "String"),
            @ApiImplicitParam(name = "ascriptionId", value = "客户移交后归属人cardId", paramType = "Integer"),
            @ApiImplicitParam(name = "customerState", value = "1:独占客户，2:共享客户", required = true, paramType = "Integer"),
    })
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel moveCustomer(@RequestAttribute CustomerRequest request) {
        return crmFegin.moveCustomer(request);
    }

    @ApiOperation(value = "企业员工退出企业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardId", value = "退出员工名片id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "userId", value = "退出员工userId", required = true, paramType = "Integer"),
    })
    @PostMapping("quitCompany")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel quitCompany(@RequestAttribute BusinessCardRequest request) {
        if (Objects.isNull(request.getCardId())) {// todo fix crm-api
            BusinessCard cardModel = businessCardBaseService.getByUserId(request.getUserId());
            if (Objects.nonNull(cardModel)) {
                request.setCardId(cardModel.getCardId());
            }
        }
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();
        return companyFeign.quit(CompanyQuitRequest.builder()
                .cardId(request.getCardId())
                .myCardId(optUser.getCardId())
                .build());
    }

    @PostMapping("dissolutionCompany")
    @ApiOperation(value = "解散企业")
    @SecurityAuthCheck(company = true, card = true, isBoss = true)
    public ResultModel dissolutionCompany() {
        RequestContext.User user = RequestContextHolder.getRequestContext().getUser();
        CompanyDissolutionRequest companyDissolutionRequest = CompanyDissolutionRequest.builder()
                .companyId(user.getCompanyId())
                .build();
        ResultModel resultModel = companyFeign.dissolution(companyDissolutionRequest);
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

}
